/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeanAdmin;

import entity.Category;
import entity.Complaint;
import entity.Department;
import entity.Members;
import entity.Solution;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import processor.ComplaintFacade;
import processor.SolutionFacade;

/**
 *
 * @author Phong
 */
@ManagedBean(name = "departmentReportMBean")
@SessionScoped
public class DepartmentReportMBean implements Serializable{

    @EJB
    private SolutionFacade solutionFacade;
    @EJB
    private ComplaintFacade complaintFacade;
    private List<Complaint> complaints;
    private List<Map.Entry<ArrayList, Complaint>> filteredDeReports;
    private List<Map.Entry<ArrayList, Complaint>> deReports;
    private List<Map.Entry<ArrayList, Complaint>> deReportsByDepartment;
    private List<Map.Entry<ArrayList, Complaint>> deReportsByCategory;
    private List<Map.Entry<ArrayList, Complaint>> deReportsByTechnician;
    private String timeSolved;

    /**
     * Creates a new instance of DepartmentReportMBean
     */
    public DepartmentReportMBean() {
    }

    public List<Map.Entry<ArrayList, Complaint>> getDeReports() {
        Map<ArrayList, Complaint> deReportsMap = new HashMap<ArrayList, Complaint>();
        for (Complaint com : getComplaints()) {
            List<Solution> soluList = solutionFacade.getSolutionByComplaintsID(com);
            for (Solution solu : soluList) {
                ArrayList al = new ArrayList();
                al.add(solu);
                al.add(getTimeSolved(com.getComplaintCreateDate(), solu.getSolutionCreateDate()));
                deReportsMap.put(al, com);
            }
        }
        Set<Map.Entry<ArrayList, Complaint>> deReportSet = deReportsMap.entrySet();
        deReports = new ArrayList<Map.Entry<ArrayList, Complaint>>(deReportSet);
        return deReports;
    }

    public List<Map.Entry<ArrayList, Complaint>> getDeReportsByDepartment(Department de) {
        Map<ArrayList, Complaint> deReportsMap = new HashMap<ArrayList, Complaint>();
        for (Complaint com : getComplaints()) {
            if (com.getCategoryID().getCategoryID() == de.getDepartmentID()) {
                List<Solution> soluList = solutionFacade.getSolutionByComplaintsID(com);
                for (Solution solu : soluList) {
                    ArrayList al = new ArrayList();
                    al.add(solu);
                    al.add(getTimeSolved(com.getComplaintCreateDate(), solu.getSolutionCreateDate()));
                    deReportsMap.put(al, com);
                }
            }
        }
        Set<Map.Entry<ArrayList, Complaint>> deReportSet = deReportsMap.entrySet();
        deReportsByDepartment = new ArrayList<Map.Entry<ArrayList, Complaint>>(deReportSet);
        return deReportsByDepartment;
    }

    public List<Map.Entry<ArrayList, Complaint>> getDeReportsByCategory(Category cate) {
        Map<ArrayList, Complaint> deReportsMap = new HashMap<ArrayList, Complaint>();
        for (Complaint com : getComplaints()) {
            if (com.getCategoryID().getCategoryID() == cate.getCategoryID()) {
                List<Solution> soluList = solutionFacade.getSolutionByComplaintsID(com);
                for (Solution solu : soluList) {
                    ArrayList al = new ArrayList();
                    al.add(solu);
                    al.add(getTimeSolved(com.getComplaintCreateDate(), solu.getSolutionCreateDate()));
                    deReportsMap.put(al, com);
                }
            }
        }
        Set<Map.Entry<ArrayList, Complaint>> deReportSet = deReportsMap.entrySet();
        deReportsByCategory = new ArrayList<Map.Entry<ArrayList, Complaint>>(deReportSet);
        return deReportsByCategory;
    }

    public List<Map.Entry<ArrayList, Complaint>> getDeReportsByTechnician(Members mem) {
        Map<ArrayList, Complaint> deReportsMap = new HashMap<ArrayList, Complaint>();
        for (Complaint com : getComplaints()) {
            List<Solution> soluList = solutionFacade.getSolutionByComplaintsID(com);
            for (Solution solu : soluList) {
                if (solu.getMembersID().getMembersID() == mem.getMembersID()) {
                    ArrayList al = new ArrayList();
                    al.add(solu);
                    al.add(getTimeSolved(com.getComplaintCreateDate(), solu.getSolutionCreateDate()));
                    deReportsMap.put(al, com);
                }
            }
        }
        Set<Map.Entry<ArrayList, Complaint>> deReportSet = deReportsMap.entrySet();
        deReportsByTechnician = new ArrayList<Map.Entry<ArrayList, Complaint>>(deReportSet);
        return deReportsByTechnician;
    }

    public String getTimeSolved(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        Calendar cal3 = Calendar.getInstance();
        cal1.setTime(date1);
        cal2.setTime(date2);
        long span = cal2.getTimeInMillis() - cal1.getTimeInMillis();
        cal3.setTimeInMillis(span);
        long numberOfMSInADay = 1000 * 60 * 60 * 24;
        timeSolved = String.valueOf(cal3.getTimeInMillis() / numberOfMSInADay);
        return timeSolved;
    }

    /**
     * @return the complaints
     */
    public List<Complaint> getComplaints() {
        complaints = complaintFacade.getComplaintAll();
        return complaints;
    }

    /**
     * @param complaints the complaints to set
     */
    public void setComplaints(List<Complaint> complaints) {
        this.complaints = complaints;
    }

    /**
     * @return the filteredDeReports
     */
    public List<Map.Entry<ArrayList, Complaint>> getFilteredDeReports() {
        return filteredDeReports;
    }

    /**
     * @param filteredDeReports the filteredDeReports to set
     */
    public void setFilteredDeReports(List<Map.Entry<ArrayList, Complaint>> filteredDeReports) {
        this.filteredDeReports = filteredDeReports;
    }

    /**
     * @param deReports the deReports to set
     */
    public void setDeReports(List<Map.Entry<ArrayList, Complaint>> deReports) {
        this.deReports = deReports;
    }

    /**
     * @return the timeSolved
     */
    public String getTimeSolved() {
        return timeSolved;
    }

    /**
     * @param timeSolved the timeSolved to set
     */
    public void setTimeSolved(String timeSolved) {
        this.timeSolved = timeSolved;
    }
}
